package org.fuxia.im.common;

import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JList;

public class CommonListCellRender extends DefaultListCellRenderer{

	private static final long serialVersionUID = 1L;

	public Component getListCellRendererComponent(JList<?> list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus) ;
		if(value.getClass().isAssignableFrom(CommonListCellData.class)) {
			CommonListCellData cData = (CommonListCellData) value ;
			if(cData.getIconPath()!=null) {
				setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(cData.getIconPath())));
			}
			if(cData.getDisplayValue()!=null) {
				setText(cData.getDisplayValue().toString()) ;
			}else {
				setText(cData.toString()) ;
			}
			return this ;
		}else {
			throw new RuntimeException("当前List使用数据模型不对");
		}
	}
	
}
